home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 17 / CU Amiga Magazine's Super CD-ROM 17 (1997)(EMAP Images)(GB)[!][issue 1997-12].iso / CUCD / Programming / DiceSource / src / shared_lib / lib.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-09-09  |  4.0 KB  |  152 lines

  1. /*
  2.  *    (c)Copyright 1992-1997 Obvious Implementations Corp.  Redistribution and
  3.  *    use is allowed under the terms of the DICE-LICENSE FILE,
  4.  *    DICE-LICENSE.TXT.
  5.  */
  6.  
  7. /*
  8.  *  LIB.C
  9.  *
  10.  *  Basic Library Resource Handling
  11.  *
  12.  *  NOTE: all data declarations should be initialized since we skip
  13.  *      normal C startup code (unless initial value is don't care)
  14.  *
  15.  *  WARNING: arguments are passed in certain registers from the assembly
  16.  *      tag file, matched to how they are declared below.  Do not change
  17.  *      the argument declarations!
  18.  */
  19.  
  20. #include "defs.h"
  21.  
  22. Prototype LibCall Library *LibInit(long);
  23. Prototype LibCall Library *LibOpen(long, Library *);
  24. Prototype LibCall long LibClose(long, Library *);
  25. Prototype LibCall long LibExpunge(long, Library *);
  26.  
  27.  
  28. Library *LibBase = NULL;    /*  Library Base pointer    */
  29. long    SegList  = 0;
  30. long    SysBase  = NULL;    /*  EXEC calls            */
  31. long    DOSBase  = NULL;    /*  if we used it ...        */
  32.  
  33. /*
  34.  *    The Initialization routine is given only a seglist pointer.  Since
  35.  *    we are NOT AUTOINIT we must construct and add the library ourselves
  36.  *    and return either NULL or the library pointer.  Exec has Forbid()
  37.  *    for us during the call.
  38.  *
  39.  *    If you have an extended library structure you must specify the size
  40.  *    of the extended structure in MakeLibrary().
  41.  */
  42.  
  43. LibCall Library *
  44. LibInit(segment)
  45. long segment;
  46. {
  47.     Library *lib;
  48.     static const long Vectors[] = {
  49.     (long)ALibOpen,
  50.     (long)ALibClose,
  51.     (long)ALibExpunge,
  52.     (long)ALibReserved,
  53.  
  54.     (long)LockTestLib,    /*  library dependant    */
  55.     (long)UnLockTestLib,
  56.     (long)PostString,
  57.     (long)GetString,
  58.  
  59.     -1
  60.     };
  61.     SysBase = *(long *)4;
  62.     DOSBase = OpenLibrary("dos.library", 0);
  63.  
  64.     LibBase = lib = MakeLibrary((APTR)Vectors,NULL,NULL,sizeof(Library),NULL);
  65.     lib->lib_Node.ln_Type = NT_LIBRARY;
  66.     lib->lib_Node.ln_Name = LibName;
  67.     lib->lib_Flags = LIBF_CHANGED|LIBF_SUMUSED;
  68.     lib->lib_Version  = 36;
  69.     lib->lib_Revision = 1;
  70.     lib->lib_IdString = (APTR)LibId;
  71.     SegList = segment;
  72.     AddLibrary(lib);
  73.  
  74.     InitC();
  75.  
  76.     return(lib);
  77. }
  78.  
  79. /*
  80.  *    Open is given the library pointer and the version request.  Either
  81.  *    return the library pointer or NULL.  Remove the DELAYED-EXPUNGE flag.
  82.  *    Exec has Forbid() for us during the call.
  83.  */
  84.  
  85. LibCall Library *
  86. LibOpen(version, lib)
  87. Library *lib;
  88. long version;
  89. {
  90.     ++lib->lib_OpenCnt;
  91.     lib->lib_Flags &= ~LIBF_DELEXP;
  92.     return(lib);
  93. }
  94.  
  95. /*
  96.  *    Close is given the library pointer and the version request.  Be sure
  97.  *    not to decrement the open count if already zero.    If the open count
  98.  *    is or becomes zero AND there is a LIBF_DELEXP, we expunge the library
  99.  *    and return the seglist.  Otherwise we return NULL.
  100.  *
  101.  *    Note that this routine never sets LIBF_DELEXP on its own.
  102.  *
  103.  *    Exec has Forbid() for us during the call.
  104.  */
  105.  
  106. LibCall long
  107. LibClose(dummy, lib)
  108. long dummy;
  109. Library *lib;
  110. {
  111.     if (lib->lib_OpenCnt && --lib->lib_OpenCnt)
  112.     return(NULL);
  113.     if (lib->lib_Flags & LIBF_DELEXP)
  114.     return(LibExpunge(0, lib));
  115.     return(NULL);
  116. }
  117.  
  118. /*
  119.  *    We expunge the library and return the Seglist ONLY if the open count
  120.  *    is zero.    If the open count is not zero we set the DELAYED-EXPUNGE
  121.  *    flag and return NULL.
  122.  *
  123.  *    Exec has Forbid() for us during the call.  NOTE ALSO that Expunge
  124.  *    might be called from the memory allocator and thus we CANNOT DO A
  125.  *    Wait() or otherwise take a long time to complete (straight from RKM).
  126.  *
  127.  *    Apparently RemLibrary(lib) calls our expunge routine and would
  128.  *    therefore freeze if we called it ourselves.  As far as I can tell
  129.  *    from RKM, LibExpunge(lib) must remove the library itself as shown
  130.  *    below.
  131.  */
  132.  
  133. LibCall long
  134. LibExpunge(dummy, lib)
  135. long dummy;
  136. Library *lib;
  137. {
  138.     if (lib->lib_OpenCnt) {
  139.     lib->lib_Flags |= LIBF_DELEXP;
  140.     return(NULL);
  141.     }
  142.     Remove(&lib->lib_Node);
  143.     FreeMem((char *)lib-lib->lib_NegSize, lib->lib_NegSize+lib->lib_PosSize);
  144.     if (DOSBase) {
  145.     CloseLibrary((Library *)DOSBase);
  146.     DOSBase = NULL;
  147.     }
  148.     UnInitC();
  149.     return((long)SegList);
  150. }
  151.  
  152.